package com.lagou.edu.service.impl;

import com.lagou.edu.dao.TokenDao;
import com.lagou.edu.entity.Token;
import com.lagou.edu.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @Author: yeqingyun
 * @Date: 2020/11/20 16:34
 */
@Service
@RequiredArgsConstructor
public class TokenServiceImpl implements TokenService {

    private final TokenDao tokenDao;

    @Override
    public void add(Token token) {
        tokenDao.save(token);
    }

    @Override
    public Optional<Token> getByToken(String token) {
        Token queryToken = new Token();
        queryToken.setToken(token);
        return tokenDao.findOne(Example.of(queryToken));
    }

    @Override
    public boolean isEmailExist(String email) {
        Token queryEmail = new Token();
        queryEmail.setEmail(email);
        return tokenDao.count(Example.of(queryEmail)) > 0;
    }
}
